package com.wanpinghui.supplier.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wanpinghui.wphlibrary.model.base.pay.PayManager;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;
    private Handler handler = new Handler() {
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, AppKeyConstant.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        PayManager.getInstance().paySuccess();
                    }
                }, 300);
            } else if (resp.errCode == -2) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        PayManager.getInstance().payFail(-100, "支付取消");
                    }
                }, 300);
            } else {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        PayManager.getInstance().payFail(0, "支付失败");
                    }
                }, 300);
            }
        }
        finish();
    }
}